package com.room.search.service;

import com.room.search.domain.RoomSecondCityList;
import com.room.search.repository.RoomCommunityRepository;
import com.room.search.repository.RoomSecondCityListRepository;
import com.room.search.response.RoomResult;
import com.room.search.service.dto.city.CityZoneGeoPointsDTO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * By   : ghd.bright
 * Time : 2022/3/14
 * Description:
 */
@Service
public class RoomSecondCityListService {

    private final RoomSecondCityListRepository roomSecondCityListRepository;
    private final RoomCommunityRepository roomCommunityRepository;

    public RoomSecondCityListService(RoomSecondCityListRepository roomSecondCityListRepository,
                                     RoomCommunityRepository roomCommunityRepository) {
        this.roomSecondCityListRepository = roomSecondCityListRepository;
        this.roomCommunityRepository = roomCommunityRepository;
    }

    public RoomResult getDistrict(String city) {
        List<CityZoneGeoPointsDTO> zoneGeoPointsDTOS = new ArrayList<>();
        List<RoomSecondCityList> roomSecondCityList = roomSecondCityListRepository.findByAdm2NameZh(city);
        roomSecondCityList.forEach(e -> {
            //每个区县下的小区数量
            Integer countCommunity = roomCommunityRepository.countAllByCityAndDistrict(city, e.getLocationNameZh());
            zoneGeoPointsDTOS.add(new CityZoneGeoPointsDTO(e.getLng(), e.getLat(), e.getAdm2NameZh(), e.getLocationNameZh(), countCommunity));

        });
        return RoomResult.success(zoneGeoPointsDTOS);
    }
}
